package col.edu.uniq.tiendamus.intprins;

import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.swing.BorderFactory;

import javax.swing.ComboBoxModel;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFormattedTextField;

import javax.swing.DefaultListModel;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.ListModel;
import javax.swing.SwingConstants;

import javax.swing.WindowConstants;
import javax.swing.border.BevelBorder;
import javax.swing.border.LineBorder;
import javax.swing.border.SoftBevelBorder;
import javax.swing.SwingUtilities;

/**
 * This code was edited or generated using CloudGarden's Jigloo
 * SWT/Swing GUI Builder, which is free for non-commercial
 * use. If Jigloo is being used commercially (ie, by a corporation,
 * company or business for any purpose whatever) then you
 * should purchase a license for each developer using Jigloo.
 * Please visit www.cloudgarden.com for details.
 * Use of Jigloo implies acceptance of these licensing terms.
 * A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
 * THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
 * LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
 */
/**
 * esta es la pantalla de inscripcion de los usuarios
 * 
 * @author Diego Sanchez, David Giraldo, Angelica Soto
 * @fecha 28 oct 2013
 * @version 2.0
 */
public class Inscribete extends javax.swing.JFrame implements ActionListener {
	private JPanel jPanelInscribete;
	private JButton jBRegTarg;
	private JLabel jLTargetasReg;
	private JList jListTargetasReg;
	private JButton jBInscribirmeRegistro;
	private JTextField jTextFFecha;
	private JLabel jLFechaExpTarg;
	private JTextField jTextFNumeroTarg;
	private JLabel jLNumeroTarg;
	private JComboBox jCombTipoTargeta;
	private JLabel jLTipoTarg;
	private JLabel jLRegTarg;
	private JTextField jTextFEmail;
	private JLabel jLEmail;
	private JTextField jTextFTel;
	private JLabel jLTel;
	private JTextField jTextFCedula;
	private JLabel jLCedula;
	private JTextField jTextFSaldo;
	private JLabel jLSaldo;
	private JTextField jTextFNombre;
	private JLabel jLNombreIns;
	private JLabel jLDatosObliInsc;
	private JLabel jLTituloInsc;
	public TiendaPrin tienda = new TiendaPrin();
	public Cliente cli;
	public Tarjeta tar;
	public SimpleDateFormat fecha = new SimpleDateFormat("dd/mm/yyyy");
	public Tienda tien = new Tienda();
	public Tarjeta tarjetas[] = new Tarjeta[3];
	private int posicion = 0;

	/**
	 * Auto-generated main method to display this JFrame
	 */

	public Inscribete() {
		super();
		initGUI();
	}

	private void initGUI() {
		try {
			setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
			{
				jPanelInscribete = new JPanel();
				getContentPane().add(jPanelInscribete, BorderLayout.CENTER);

				jPanelInscribete
						.setBackground(new java.awt.Color(85, 170, 255));
				jPanelInscribete.setLayout(null);
				jPanelInscribete.setPreferredSize(new java.awt.Dimension(455,
						685));
				{
					jLTituloInsc = new JLabel();
					jPanelInscribete.add(jLTituloInsc);
					jLTituloInsc.setText("I n s c r i b e t e");
					jLTituloInsc.setBounds(124, 5, 118, 27);
					jLTituloInsc.setHorizontalAlignment(SwingConstants.CENTER);
					jLTituloInsc.setFont(new Font("Cataneo BT", 0, 20));
				}
				{
					jLDatosObliInsc = new JLabel();
					jPanelInscribete.add(jLDatosObliInsc);
					jLDatosObliInsc.setText("* Datos obigatorios");
					jLDatosObliInsc.setBounds(12, 38, 101, 16);
				}
				{
					jLNombreIns = new JLabel();
					jPanelInscribete.add(jLNombreIns);
					jLNombreIns.setText("Nombre :  *");
					jLNombreIns.setBounds(5, 68, 65, 27);
				}
				{
					jTextFNombre = new JTextField();
					jPanelInscribete.add(jTextFNombre);
					jTextFNombre.setBounds(88, 70, 151, 23);
					jTextFNombre.addFocusListener(new FocusAdapter() {

					});
				}
				{
					jLCedula = new JLabel();
					jPanelInscribete.add(jLCedula);
					jLCedula.setText("Cedula :   * ");
					jLCedula.setBounds(5, 114, 60, 16);
				}
				{
					jTextFCedula = new JTextField();
					jPanelInscribete.add(jTextFCedula);
					jTextFCedula.setBounds(88, 111, 151, 23);
				}
				{
					jLTel = new JLabel();
					jPanelInscribete.add(jLTel);
					jLTel.setText("Telefono :  *");
					jLTel.setBounds(8, 149, 64, 16);
				}
				{
					jTextFTel = new JTextField();
					jPanelInscribete.add(jTextFTel);
					jTextFTel.setBounds(90, 146, 149, 23);
				}
				{
					jLEmail = new JLabel();
					jPanelInscribete.add(jLEmail);
					jLEmail.setText("E-mail :  *");
					jLEmail.setBounds(8, 192, 51, 16);
				}
				{
					jTextFEmail = new JTextField();
					jPanelInscribete.add(jTextFEmail);
					jTextFEmail.setBounds(88, 189, 151, 23);
				}
				{
					jLRegTarg = new JLabel();
					jPanelInscribete.add(jLRegTarg);
					jLRegTarg.setText("Registrar Tarjetas max 3 :");
					jLRegTarg.setBounds(22, 272, 177, 16);
				}
				{
					jLTipoTarg = new JLabel();
					jPanelInscribete.add(jLTipoTarg);
					jLTipoTarg.setText("Tipo :  *");
					jLTipoTarg.setBounds(59, 313, 65, 21);
				}
				{
					ComboBoxModel jCombTipoTargetaModel = new DefaultComboBoxModel(
							new String[] { "Debito", "Credito" });
					jCombTipoTargeta = new JComboBox();
					jPanelInscribete.add(jCombTipoTargeta);
					jCombTipoTargeta.setModel(jCombTipoTargetaModel);
					jCombTipoTargeta.setBounds(114, 312, 85, 23);
					jCombTipoTargeta.addActionListener(new ActionListener() {
						public void actionPerformed(ActionEvent evt) {
							jCombTipoTargetaActionPerformed(evt);
						}
					});
				}
				{
					jLNumeroTarg = new JLabel();
					jPanelInscribete.add(jLNumeroTarg);
					jLNumeroTarg.setBounds(42, 360, 72, 21);
					jLNumeroTarg.setText("Numero :  *");
				}
				{
					jTextFNumeroTarg = new JTextField();
					jPanelInscribete.add(jTextFNumeroTarg);
					jTextFNumeroTarg.setBounds(114, 359, 85, 23);
				}
				{
					jLFechaExpTarg = new JLabel();
					jPanelInscribete.add(jLFechaExpTarg);
					jLFechaExpTarg.setText("Fecha Exp :  *");
					jLFechaExpTarg.setBounds(35, 468, 79, 16);
				}
				{
					jTextFFecha = new JTextField();
					jPanelInscribete.add(jTextFFecha);
					jTextFFecha.setBounds(114, 465, 85, 23);
				}
				{
					jBInscribirmeRegistro = new JButton();
					jPanelInscribete.add(jBInscribirmeRegistro);
					jBInscribirmeRegistro.setText("INSCRIBIRME");
					jBInscribirmeRegistro.setBounds(145, 553, 167, 95);
					jBInscribirmeRegistro
							.setFont(new Font("Cataneo BT", 1, 19));
					jBInscribirmeRegistro.setBorder(BorderFactory
							.createBevelBorder(BevelBorder.LOWERED));
					jBInscribirmeRegistro
							.addActionListener(new ActionListener() {
								public void actionPerformed(ActionEvent evt) {
									jBInscribirmeRegistroActionPerformed(evt);
								}
							});
				}
				{
					ListModel jListTargetasRegModel = new DefaultComboBoxModel(
							new String[] { "Item One", "Item Two" });
					jListTargetasReg = new JList();
					jPanelInscribete.add(jListTargetasReg);
					jListTargetasReg.setModel(jListTargetasRegModel);
					jListTargetasReg.setBounds(293, 312, 124, 172);
					jListTargetasReg.setFont(new Font("Cataneo BT", 0, 15));
					jListTargetasReg.setBorder(new SoftBevelBorder(
							BevelBorder.LOWERED, null, null, null, null));
				}
				{
					jLTargetasReg = new JLabel();
					jPanelInscribete.add(jLTargetasReg);
					jLTargetasReg.setText("Targetas registradas ");
					jLTargetasReg.setBounds(287, 275, 138, 25);
					jLTargetasReg.setFont(new Font("Cataneo BT", 0, 15));
				}
				{
					jBRegTarg = new JButton();
					jPanelInscribete.add(jBRegTarg);
					jBRegTarg.setText(">>");
					jBRegTarg.setBounds(214, 465, 49, 23);
					jBRegTarg.setHorizontalAlignment(SwingConstants.LEADING);
					jBRegTarg.addActionListener(new ActionListener() {
						public void actionPerformed(ActionEvent evt) {
							jBRegTargActionPerformed(evt);
						}
					});
				}
				{
					jLSaldo = new JLabel();
					jPanelInscribete.add(jLSaldo);
					jLSaldo.setText("Saldo : *");
					jLSaldo.setBounds(54, 413, 60, 16);
				}
				{
					jTextFSaldo = new JTextField();
					jPanelInscribete.add(jTextFSaldo);
					jTextFSaldo.setBounds(114, 410, 85, 23);
				}
			}
			pack();
			this.setSize(471, 723);
		} catch (Exception e) {
			// add your error handling code here
			e.printStackTrace();
		}
	}

	@Override
	public void actionPerformed(ActionEvent arg0) {

	}

	private void jCombTipoTargetaActionPerformed(ActionEvent evt) {

	}

	private void jBRegTargActionPerformed(ActionEvent evt) {

		Date fechav = null;

		if (jTextFNumeroTarg.getText() == null || jTextFFecha.getText() == null) {
			JOptionPane.showMessageDialog(null, "faltan datos en las casillas");
		} else {

			Double saldoNum = Double.parseDouble(jTextFSaldo.getText());

			try {
				fechav = fecha.parse(jTextFFecha.getText());

				tar = new Tarjeta(jTextFNumeroTarg.getText(), fechav);
				tarjetas[posicion] = tar;

				DefaultListModel modeloLista = new DefaultListModel();
				jListTargetasReg.setModel(modeloLista);
				modeloLista.addElement(tar);
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

	}

	private void jBInscribirmeRegistroActionPerformed(ActionEvent evt) {

		if (jTextFNombre.getText() == null || jTextFCedula.getText() == null
				|| jTextFTel.getText() == null || jTextFEmail.getText() == null) {
			JOptionPane.showMessageDialog(null,
					"falta por lo menos una casilla por llenar");
		} else {
			cli = new Cliente(jTextFNombre.getText(),
					jTextFCedula.getText(),
					jTextFTel.getText(),
					jTextFEmail.getText(), tarjetas);
			tien.ingresarCliente(cli);

		}
	}

}
